const log4js = require("log4js");
const path = require("path");

log4js.configure({
  replaceConsole: true,
  appenders: {
    console: {
      type: "stdout",
      level: "INFO",
    },
    http: {
      type: "dateFile",
      filename: path.resolve(__dirname, "../logs", "http.log"),
      alwaysIncludePattern: true,
      maxLogSize: 10 * 1024 * 1024,
      keepFileExt: true,
    },
    errorLog: {
      type: "file",
      filename: path.resolve(__dirname, "../logs", "error.log"),
      maxLogSize: 10 * 1024 * 1024,
      keepFileExt: true,
    },
    error: { 
      type: "logLevelFilter",
      level: "info",
      appender: 'errorLog',
    },
    sql: {
      type: "dateFile",
      filename: path.resolve(__dirname, "../logs", "sql", "logging.log"),
      keepFileExt: true,
      daysToKeep: 30,
      layout: {
        type: "pattern",
        pattern: "%c [%d{yyyy-MM-dd hh:mm:ss}] [%p]: %m%n",
      },
    },
  },
  categories: {
    console: {
      appenders: ["console"],
      level: "off",
    },
    default: {
      appenders: ["console"],
      level: "off",
    },
  },
});

const logger = log4js.getLogger();

// 覆盖配置中的 level，如果没有配置，则必须配置设置
logger.level = "debug";

module.exports = {
  logger,
  connectLogger: log4js.connectLogger(logger),
};
